<template>
	<view class="add">
		<view v-show="!boxHide">
			<view class="add_item" @click.stop="openSearch('customer')">
				<view>客户选择</view>
				<input type="text" v-model="selectUsers.customer" disabled placeholder="请选择客户" />
				<image src="/static/images/right.png" class="right"></image>
			</view>
			<view class="add_item">
				<view>订单号</view>
				<input type="number" v-model="order_no" placeholder="如不填写后台自动生成" />
			</view>
			<view class="add_item">
				<view>工单类型</view>
				<picker mode="selector" :range="workType" :data-name="'workType'" :value="workType_index" @change="getLaber" class="add_picker">
					<text>{{workTypetext}}</text>
					<image src="/static/images/right.png" class="right"></image>
				</picker>
			</view>
			<!-- <view class="add_item">
				<view>工时费</view> 
				<input type="digit" v-model="price" placeholder="请输入工时费" />
			</view> -->
			<view class="add_item" v-if="status !=1">
				<view class="">预约时间</view>
				<view class="add_picker">
					<Time ref='time' :fields="'second'" :lang="false" @getDate="getStartDate" :time="reserve_time"></Time>
				</view>
			</view>
			<view class="add_item" @click.stop="openSearch('zxr_user')">
				<view>执行人</view>
				<input type="text" v-model="selectUsers.zxr" disabled placeholder="请选择执行人" />
				<image src="/static/images/right.png" class="right"></image>
			</view>
			<view class="add_item" @click.stop="openSearch('fs_user')">
				<view>副手</view>
				<input type="text" v-model="selectUsers.fs" disabled placeholder="请选择副手" />
				<image src="/static/images/right.png" class="right"></image>
			</view>
			<view class="add_item" @click.stop="openSearch('all_user')">
				<view>回访人</view>
				<input type="text" v-model="selectUsers.visit" disabled placeholder="请选择回访人" />
				<image src="/static/images/right.png" class="right"></image>
			</view>
			<view class="add_item">
				<view>派单人</view>
				<text>{{selectUsers.pdr}}</text>
			</view>

			<view class="add_textarea">
				<view>具体内容</view>
				<textarea :value="content" placeholder="请输入具体内容" :data-name="'content'" @input="getLaber" />
				</view>
			<view class="btn-list">
				<view class="save" @click.stop="sava(3)" v-if="status==3">保存</view>
				<view class="save" @click.stop="sava(1)" v-if="status==1">确认派单</view>
				<view class="save" @click.stop="sava(4)" v-if="status==4">确认派单</view>
			</view>
		</view>
		<Loading ref="load"></Loading>
		
		
	</view>
</template>

<script>
	import Time from "../../../components/time/time.vue";
	import Loading from "../../../components/loading/loading.vue";
	import {
		deepClone
	} from '@/static/js/index.js'
	export default{
		components:{
			Time,
			Loading
		},
		data(){
			return{
				status:0,
				boxHide: false,
				workType:[],		// 工单类型
				workTypetext: "",   // 显示类型
				workType_index:0,
				selectUsers:{
					customer:"",//客户
					customerId:"",//客户id
					zxr:"",//执行人
					zxrId:"",//执行人id
					fs:"",//副手
					fsId:"",//副手id
					visit:"",//回访人
					visitId:"",//回访人id
					pdr:"",//派单员
					pdrId:"",//派单员Id
				},
				reserve_time: '', // 预约时间
				content: '',   
				customerName:"",	// 客户名
				customerNameId:''	,// 客户id
				order_no:"",
				price:"",
				workOrderId:""
			}
		},
		onLoad(options) {
			this.status=options.status;
			this.setNavTitle();
			this.workOrderId=options.workOrderId?options.workOrderId:'';
			this.boxHide=options.workOrderId?true:false;
			this.getJson();
			this.getDetail();
		},
		onShow() {
			this.setBaseNews();
		},
		methods:{
			//设置基础信息
			setBaseNews(){
				let obj=deepClone(this.$store.getters['parameters/searchObj']);
				// console.log('obj====',obj)	
				if(obj.key=="customer"){
					this.selectUsers.customer=obj.data.title;
					this.selectUsers.customerId=obj.data.id;

					this.defualtZXR();
				} else if(obj.key=="zxr_user"){
					this.selectUsers.zxr=obj.data.username;
					this.selectUsers.zxrId=obj.data.id;
				}else if(obj.key=="fs_user"){
					this.selectUsers.fs=obj.data.username;
					this.selectUsers.fsId=obj.data.id;
				}else if(obj.key=="all_user"){
					this.selectUsers.visit=obj.data.username;
					this.selectUsers.visitId=obj.data.id;
				}
				this.$store.dispatch('parameters/deleteObj');
			},
			//打开搜索列表
			openSearch(key){
				uni.navigateTo({
					url:'/pages/searchList/searchList?searchKey='+key
				})
			},
			setNavTitle(){
				uni.setNavigationBarTitle({
					title:this.status==3?'预约单':'派单'
				})
			},
			// 获取工单详情
			getDetail(){
				if(!this.workOrderId) {
					this.selectUsers.pdr=this.$store.getters.userInfo.username;
					this.selectUsers.pdrId=this.$store.getters.userInfo.id;
					return false;
				};
				this.$refs.load.initLoading({
					loadType: 1
				}).then(res => {
					this.$api.getWorkDetail({user_id:uni.getStorageSync("userId") ,id:this.workOrderId}).then(res => {
						this.boxHide = false;
						this.setDetailsValue(res);
						this.$refs.load.getLoadingData({})
					})
				})
			},
			//设置详情值
			setDetailsValue(data){
				this.selectUsers.customer=data.customer;
				this.selectUsers.customerId=data.customer_id;
				this.order_no=data.order_no;
				this.workTypetext=data.type;
				this.reserve_time=this.$refs.time.date=data.reserve_time;
				this.selectUsers.zxr=data.exec_user;
				this.selectUsers.zxrId=data.exec_user_id;
				this.selectUsers.fs=data.helper_user;
				this.selectUsers.fsId=data.helper_user_id;
				this.selectUsers.visit=data.visit_user;
				this.selectUsers.visitId=data.visit_user_id;
				this.selectUsers.pdr=data.salesman_user;
				this.selectUsers.pdrId=data.salesman_user_id;
				this.content=data.content;
			},
			// 默认执行人
			defualtZXR(){
				let upJson = {id: this.selectUsers.customerId,user_id: uni.getStorageSync("userId")};

				this.$api.getClientDetails(upJson,0).then( res => {
					this.selectUsers.zxr = res.sale_user;
					//this.selectUsers.zxrId = res.contact_user_id;
					this.selectUsers.zxrId = res.sale_user_id;
				})
			},
			getJson(){
				this.$api.getWorkType().then(res => { // 类型
					this.workType = res;
					this.workTypetext = res[0];
				})
			},
			getLaber(e){
				if(e.currentTarget.dataset.name == "workType"){
					this.workTypetext = this.workType[e.detail.value]
				} 
				if(e.currentTarget.dataset.name == "content"){
					this.content = e.detail.value
				}
			},
			getStartDate(e){
				this.reserve_time = e.date
			},
			go(){
				uni.navigateTo({
					url:'/pages/work/add/cusomer/cusomer'
				})
			},
			sava(status){
				let that = this;
				let upjson = {
					customer_id:this.selectUsers.customerId,
					order_no:this.order_no,
					type: this.workTypetext,
					exec_user_id:this.selectUsers.zxrId,
					helper_user_id:this.selectUsers.fsId,
					visit_user_id:this.selectUsers.visitId,
					salesman_user_id:this.selectUsers.pdrId,
					reserve_time:this.reserve_time,
					content: this.content,
					status:status,
				}
				if(status==4){
					upjson.id=this.workOrderId;
				}
				if(this.order_no.length > 0)upjson.order_no = this.order_no
				if(!upjson.customer_id){
					uni.showToast({title:"请选择客户",icon:"none"})
				} else if(status!=3&&!upjson.exec_user_id){
					uni.showToast({title:"请选择执行人",icon:"none"})
				}
				/*else if(status!=3&&!upjson.helper_user_id){
					uni.showToast({title:"请选择副手",icon:"none"})
				}else if(status!=3&&!upjson.visit_user_id){
					uni.showToast({title:"请选择回访人",icon:"none"})
				}else if(upjson.content.length <= 0){
					uni.showToast({title:"请添加具体内容",icon:"none"})
				}*/
				else{
					let resMsg=status==3?'保存成功':'派单成功';
					that.$api.getWorkCreact(upjson,resMsg)
					this.$app.globalData.upData.work = true;
				}
				
			}
		}
	}
</script>

<style>
	.btn-list{display: flex;padding: 0 15rpx;}
	.save{margin: 0 15rpx;}
	.add{width: 100%;background: #FFFFFF;padding-bottom: 50upx;}
	.add_item_text{color: #666666;}
</style>
